package com.durian.controller;

import com.durian.domain.DTO.ObsDefaultListDTO;
import com.durian.domain.DTO.ObsShopcarAddDTO;
import com.durian.domain.DTO.ObsShopcarUpdateDTO;
import com.durian.service.IObsShopcarService;
import com.durian.utils.Result;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车管理
 *
 * @author Durian
 * @since 2023-11-23
 */
@RestController
@Validated
@RequestMapping("book/shopcar")
@RequiredArgsConstructor
public class ObsShopcarController {

    private final IObsShopcarService shopcarService;

    /**
     * 添加购物车
     */
    @PostMapping("add")
    public Result add(
            @Validated
            @RequestBody
            ObsShopcarAddDTO shopcarAddDTO
    ) {
        shopcarService.addShopCar(shopcarAddDTO);
        return Result.success();
    }

    /**
     * 移除购物车
     */
    @PutMapping("remove")
    public Result remove(
            @RequestBody
            List<@Positive(message = "id格式有误") Long> ids
    ) {
        shopcarService.removeShopcar(ids);
        return Result.success();
    }

    /**
     * 更新购物车
     */
    @PutMapping("update")
    public Result updateShopcar(
            @RequestBody
            ObsShopcarUpdateDTO shopcarUpdateDTO
    ) {
        shopcarService.updateShopcar(shopcarUpdateDTO);
        return Result.success();
    }

    /**
     * 获取购物车列表
     */
    @PostMapping("list")
    public Result listShopcar(
            @RequestBody
            ObsDefaultListDTO defaultListDTO
    ) {
        return Result.success(shopcarService.listShopcar(defaultListDTO));
    }

    /**
     * 根据用户id获取购物车列表
     */
    @PostMapping("list/{userId}")
    public Result ListShopcarById(
            @Positive(message = "id格式有误")
            @PathVariable Long userId
    ) {
        return Result.success(shopcarService.getShopcar(userId));
    }
}
